﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class PointGift
    {
        private readonly Chain.IDAL.PointGift dal = new Chain.IDAL.PointGift();

        public int Add(Chain.Model.PointGift model)
        {
            if (this.ExistsAdd(model.GiftName, model.GiftShopID))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public List<Chain.Model.PointGift> DataTableToList(DataTable dt)
        {
            List<Chain.Model.PointGift> list = new List<Chain.Model.PointGift>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.PointGift item = new Chain.Model.PointGift();
                    if ((dt.Rows[i]["GiftID"] != null) && (dt.Rows[i]["GiftID"].ToString() != ""))
                    {
                        item.GiftID = int.Parse(dt.Rows[i]["GiftID"].ToString());
                    }
                    if ((dt.Rows[i]["GiftName"] != null) && (dt.Rows[i]["GiftName"].ToString() != ""))
                    {
                        item.GiftName = dt.Rows[i]["GiftName"].ToString();
                    }
                    if ((dt.Rows[i]["GiftCode"] != null) && (dt.Rows[i]["GiftCode"].ToString() != ""))
                    {
                        item.GiftCode = dt.Rows[i]["GiftCode"].ToString();
                    }
                    if ((dt.Rows[i]["GiftClassID"] != null) && (dt.Rows[i]["GiftClassID"].ToString() != ""))
                    {
                        item.GiftClassID = int.Parse(dt.Rows[i]["GiftClassID"].ToString());
                    }
                    if ((dt.Rows[i]["GiftPhoto"] != null) && (dt.Rows[i]["GiftPhoto"].ToString() != ""))
                    {
                        item.GiftPhoto = dt.Rows[i]["GiftPhoto"].ToString();
                    }
                    if ((dt.Rows[i]["GiftExchangePoint"] != null) && (dt.Rows[i]["GiftExchangePoint"].ToString() != ""))
                    {
                        item.GiftExchangePoint = int.Parse(dt.Rows[i]["GiftExchangePoint"].ToString());
                    }
                    if ((dt.Rows[i]["GiftStockNumber"] != null) && (dt.Rows[i]["GiftStockNumber"].ToString() != ""))
                    {
                        item.GiftStockNumber = int.Parse(dt.Rows[i]["GiftStockNumber"].ToString());
                    }
                    if ((dt.Rows[i]["GiftExchangeNumber"] != null) && (dt.Rows[i]["GiftExchangeNumber"].ToString() != ""))
                    {
                        item.GiftExchangeNumber = int.Parse(dt.Rows[i]["GiftExchangeNumber"].ToString());
                    }
                    if ((dt.Rows[i]["GiftShopID"] != null) && (dt.Rows[i]["GiftShopID"].ToString() != ""))
                    {
                        item.GiftShopID = int.Parse(dt.Rows[i]["GiftShopID"].ToString());
                    }
                    if ((dt.Rows[i]["GiftRemark"] != null) && (dt.Rows[i]["GiftRemark"].ToString() != ""))
                    {
                        item.GiftRemark = dt.Rows[i]["GiftRemark"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int GiftID)
        {
            return this.dal.Delete(GiftID);
        }

        public bool DeleteList(string GiftIDlist)
        {
            return this.dal.DeleteList(GiftIDlist);
        }

        public bool Exists(string GiftName, int GiftID, int shopID)
        {
            return this.dal.Exists(GiftName, GiftID, shopID);
        }

        public bool ExistsAdd(string GiftName, int shopID)
        {
            return this.dal.ExistsAdd(GiftName, shopID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetItemModel(int GiftID)
        {
            return this.dal.GetItemModel(GiftID);
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListS(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListS(PageSize, PageIndex, strWhere, out resCount);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, bool IsAsc, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, IsAsc, strWhere, out resCount);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.PointGift GetModel(int GiftID)
        {
            return this.dal.GetModel(GiftID);
        }

        public List<Chain.Model.PointGift> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public int Update(Chain.Model.PointGift model)
        {
            if (this.Exists(model.GiftName, model.GiftID, model.GiftShopID))
            {
                return -1;
            }
            return this.dal.Update(model);
        }

        public int UpdateGiftNumber(int giftID, int number)
        {
            return this.dal.UpdateGiftNumber(giftID, number);
        }
    }
}

